package com.atguigu.tingshu.user.strategy.factory;

import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.user.strategy.PaidRecordStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 策略工厂:管理所有策略实现类对象，根据策略类型获取对应的策略实现类对象
 */
@Slf4j
@Component
public class StrategyFactory {

    /**
     * 自动注入方式为map，key为策略类型，value为策略实现类对象
     */
    @Autowired
    private Map<String, PaidRecordStrategy> strategyMap;

    /**
     * 根据策略类型获取对应策略实现类对象
     *
     * @param strategy
     * @return
     */
    public PaidRecordStrategy getStrategy(String strategy) {
        if (strategyMap.containsKey(strategy)) {
            return strategyMap.get(strategy);
        }
        throw new RuntimeException("策略类型不存在");
    }
}

